<?php

class IndexController extends API_Controller {
    public function init(){
        parent::init();
        //self::setTitle('Главная страница');
    }
    
    public function indexAction() {
        if(API_Auth::isAuth())
            self::go("/client/");
        self::setTitle('Авторизация');                              
        self::parse();                           
    }
    
    public function loginAction() {                   
        if(self::getRequest()->isPost()){                 
            $acc = intval(self::getRequest()->getPost('acc'));
            API_Auth::doAuth($acc);
        }
        self::go("/");
    }
    
    public function logoutAction() {     
        API_Auth::doExit();
        self::go("/");
    }
    
    public function getdraftAction(){
        $ac = API_Clients::normAcc(self::getPar(0));        
        $user = API_Clients::getClientByAcc($ac);
        if(!$user)
            die("0");
            
        $drawto = API_Utils::loadJson('draft');                       
        
        $mm = (int)date("m");
       // $mm--;
        $yy = (int)date("Y");
        /*if($mm==0){
            $mm = 12;
            $yy--;
        }*/
        
        $money = API_Clients::getPlatMoney($ac);
        if(!$money)
            die("1");

        $nums = API_Clients::getPlatNums($ac);
        if(!$nums){
            $nums = array(array('NUM' => '--------------', 'DUR'=>'', 'COST'=>''));
            //die("2");
        }
        
        $data = array('fio' => $user['FIO']." (".$user['ACCOUNT_NUMBER'].")",
                      'address' => $user['ADDRESS'],
                      'phones' => array(), //array('num'=>32523523,'min'=>99,'price'=>32.5), array('num'=>3213523,'min'=>8,'price'=>777.53),array('num'=>355523523,'min'=>1,'price'=>0.5)),
                      'month' => API_Utils::getLocMonth($mm).' '.$yy,
                      'monthp' => API_Utils::getLocMonth($mm, 'p').' '.$yy,
                      'now' => str_replace('*', API_Utils::getLocMonth(time(), 'r'), date("d * Y")),
                      'debt' => $money['ONE'],
                      'calced' => $money['TWO'],
                      'payed' => $money['THREE'],
                      'total' => $money['FOUR'],
                      );
        foreach($nums as $num){
            $data['phones'][] = array('num'=>$num['NUM'], 'min'=>$num['DUR'], 'price'=>$num['COST']);
        }
        
        $iw = new API_ImageWriter();
        $iw->Open('draft.png');
        $iw->SetColor('#000000');
        $iw->SetFont('verdana', false, 9);
        //$iw->WriteText(10, 10, 'asd');
        $iw->WriteArray($data, $drawto);
        if($this->getResponse()->canSendHeaders())
            $this->getResponse()->setHeader('Content-Type', 'image/jpg');
        $iw->Out('jpg');
    }
    
    /*public function infoAction() {
        phpinfo();
    }*/
}
